package net.adetech.dcweb.core.dc.service.impl;

import net.adetech.dcweb.core.dc.service.IDcSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;

@Service
public class DcSmsServiceImpl implements IDcSmsService {

    @Autowired
    @Qualifier("oracleJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Value("${aliyun_sms.switch}")
    private String smsSwitch;

    @Override
    public void sendMessage(String phone, String content) {
        if (!("on".equals(smsSwitch)||"true".equals(smsSwitch)))
        {
            System.out.println("短信服务未开启"+smsSwitch);
            return;
        }

        Timestamp now = new Timestamp(new Date().getTime());
        final String sql = "insert into hge_ftp.dc_sms (id, content, phone, sms_time)" +
                "values (hge_ftp.SEQ_DC_SMS_ID.NEXTVAL, ?, ?, ?)";
        try{
            jdbcTemplate.update(sql,new Object[]{content,phone,now});
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public String getSmsSwitch() {
        return smsSwitch;
    }

    public void setSmsSwitch(String smsSwitch) {
        this.smsSwitch = smsSwitch;
    }
}
